package com.wlyuan.index.application.translator;

import com.wlyuan.index.api.vo.CombineIndexScrollVO;
import com.wlyuan.index.api.vo.CombineIndexVO;
import com.wlyuan.index.domain.index.CombineIndex;
import com.wlyuan.index.domain.index.CombineIndexScroll;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;
import java.util.Optional;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class CombineIndexTranslator {
    @Mapper
    public interface Translator {
        CombineIndexVO translate(CombineIndex source);

        default BigDecimal format(BigDecimal volume) {
            return Optional.ofNullable(volume)
                    .map(BigDecimal::stripTrailingZeros)
                    .map(BigDecimal::toPlainString)
                    .map(BigDecimal::new)
                    .orElse(null);
        }

        CombineIndexScrollVO translate(CombineIndexScroll source);
    }

    private static final Translator TRANSLATOR = Mappers.getMapper(Translator.class);

    public static CombineIndexVO translate(CombineIndex source) {
        return TRANSLATOR.translate(source);
    }

    public static CombineIndexScrollVO translate(CombineIndexScroll source) {
        return TRANSLATOR.translate(source);
    }
}
